#!/bin/sh

############################################################################
#    Copyright (C) 2010 by Cedric Nilly aka lordgun                        #
#    cedric@nilly.fr                                                       #
#                                                                          #
#    This program is free software; you can redistribute it and#or modify  #
#    it under the terms of the GNU General Public License as published by  #
#    the Free Software Foundation; either version 2 of the License, or     #
#    (at your option) any later version.                                   #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU General Public License for more details.                          #
#                                                                          #
#    You should have received a copy of the GNU General Public License     #
#    along with this program; if not, write to the                         #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################

# 0.3.7 Conf file + s3clean) 
# 0.3.6 Mysqldump bugfix; bugfix clean()
# 0.3.5 Splitting files

CONF="/root/lord_backup.conf"


. $CONF || exit 0 

echo $LOG

DATE=`date +%Y%m%d`
SQL=sql
WEB=www


case $1 in

	www)
		#### BACKUP WWW

		mkdir -p $BACKUP_DIRECTORY/$DATE/$WEB 2>&1 >/dev/null	
		cd $BACKUP_DIRECTORY/$DATE/$WEB
	
		for i in `ls -1d $NEED_BACKUP`
		do
		    DATE_START=`date +%s`
		    FILENAME=`echo $i | sed "s/\//_/g"`
		    echo -n "    www - backuping $i to $FILENAME" 
		    tar jcvf $FILENAME.tar.bz2 $i 2>/dev/null >/dev/null
		    DATE_FINISH=`date +%s`
		    echo -n " ["; echo $DATE_FINISH-$DATE_START | bc | tr "\n" " "; echo "s]"
		done
		;;

	sql)

		#### BACKUP SQL

		mkdir -p $BACKUP_DIRECTORY/$DATE/$SQL 2>&1 >/dev/null
		cd $BACKUP_DIRECTORY/$DATE/$SQL

		for i in `echo "show databases" | mysql -u $MYSQL_USER -h $MYSQL_HOST -p$MYSQL_PASSWORD | grep -v "^Database$"`
		do
		    DATE_START=`date +%s`
		    echo -n "    sql - backuping $i"
		    mysqldump -Q --opt -u $MYSQL_USER -h $MYSQL_HOST -p$MYSQL_PASSWORD --databases $i 2>>$LOG | bzip2 -c >$i.sql.bz2
		    DATE_FINISH=`date +%s`
		    echo -n " ["; echo $DATE_FINISH-$DATE_START | bc | tr "\n" " "; echo "s]"
		done
		;;

        split)
		for FILE in `find $BACKUP_DIRECTORY/$DATE/ -type f -size +$MAX_FILESIZE_MB` ; do
		    echo "    splitting $FILE";
                    split -b$MAX_FILESIZE_MB $FILE $FILE. && rm $FILE
		done
		;;

	s3up)

		# time s3cmd sync lordbackup s3://bayard-presse-backup-ovh1
		cd $BACKUP_DIRECTORY/..
		s3cmd sync $BACKUP_DIRECTORY/$DATE/ s3://$BUCKET_NAME/$DATE/ 2>>$LOG
		;;

    s3clean)

	    # clean the last month's files except the first day of the month
		last=`date --date="last month" +%Y%m`
		echo "    removing old directories";
		for i in `seq -w 2 31`
			do s3cmd del --recursive s3://$BUCKET_NAME/$last$i
		done
		;;

	clean)

		find $BACKUP_DIRECTORY/ -maxdepth 1 -mtime +2 -exec rm {} --force --recursive \;
		;;

	backup)

		echo "`date` Lordbackup: clean" >>$LOG;		$0 clean 2>&1 >>$LOG;
		echo "`date` Lordbackup: s3clean" >>$LOG;     $0 clean 2>&1 >>$LOG;
		echo "`date` Lordbackup: backup www" >>$LOG;	$0 www 2>&1 >>$LOG;
		echo "`date` Lordbackup: backup sql" >>$LOG;    $0 sql 2>&1 >>$LOG;
		echo "`date` Lordbackup: split" >>$LOG;		$0 split 2>&1 >>$LOG;
		echo "`date` Lordbackup: s3up" >>$LOG;		$0 s3up 2>&1 >>$LOG;
		echo "`date` Lordbackup: end." >>$LOG;
		;;

	*)
		echo " www        backup dictories and files"
		echo " sql        mysqldump all databases (one .bz2 per database)"
		echo " s3up       upload backups to Amazon S3"
		echo " clean      delete local old backups (2 days old)"
		echo " s3clean    delete old month's backup (except the first day of each month)"
		echo " backup     clean & s3clean & www & sql & split & s3up"

esac

